// pages/sendMsg/sendMsg.js
import api from '../../utils/api.js'
var app = getApp();
import {
    HTTP
} from '../../utils/http.js'
let http = new HTTP()
import { config } from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        height: app.globalData.height * 2 + 20,
        imgs: [], //上传图片的地址列表
        load: true, //上传图片的显隐
        msg: "", //留言
        imgUrl: config.imgUrl,
    },
    //接口
    //专题留言接口
    addMessage(obj) {
        http.request({
            url: api.getaddMessage,
            data: obj,
            success: res => {
                if (res.code == 200) {
                    wx.showToast({
                        title: '留言成功',
                        duration: 1500,
                        mask: true,
                    })
                    setTimeout(() => {
                        wx.navigateBack({})
                    }, 1000)
                }
            }
        })
    },


    //删除图片
    del(e) {
        // console.log(this.data.images)
        var imgs = this.data.imgs
        var images = this.data.images
        imgs.splice(e.target.dataset.id, 1)
            // images.splice(e.target.dataset.id, 1)
        this.setData({
            imgs,
            // images,
            load: true
        })
    },
    //上传图片
    loadImg() {
        var that = this;
        wx.chooseImage({
            count: 3, //最多可以选择的图片总数  
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: (res) => {
                var tempFilePaths = res.tempFilePaths;
                var imgs = this.data.imgs
                imgs.push(tempFilePaths[0])
                this.setData({
                    imgs,
                })
                if (imgs.length == 3) {
                    this.setData({
                        load: false
                    })
                }
            }
        });
    },
    //输入留言
    msg(e) {
        this.setData({
            msg: e.detail.value
        })
    },
    //友邻吧——确认发布
    confirm() {
        let that = this;
        let city_id = wx.getStorageSync('city_id');
        let user_id = wx.getStorageSync('user_id');
        let images = this.data.imgs;
        let onLineimages = []; //线上 图片 路径
        let count = 0; //本地图片计数

        if (!this.data.msg) {
            wx.showToast({
                title: '请填写你的动态',
                icon: "none",
                mask: true,
            })
            return
        }

        if (this.data.imgs.length == 0) {
            console.log(this.data.imgs);
            wx.showToast({
                title: '请至少添加一张图片',
                icon: "none",
                mask: true,
            })
            return
        }

        if (images.length) {
            // 先上传图片
            wx.showToast({
                title: '正在上传图片...',
                icon: 'loading',
                mask: true,
                duration: 1500
            })
            for (let i = 0, h = images.length; i < h; i++) {
                console.log('images', images[i])
                    //上传文件
                wx.uploadFile({
                    url: "https://xiangtws.com/public/index.php/api/Status/uploadImages",
                    filePath: images[i],
                    name: 'images[]', //与后台 命名的数组对应
                    success: (res) => {
                        console.log(res)
                        onLineimages.push(res.data)
                        this.setData({
                            onLineimages
                        })
                        count++;
                        //如果是最后一张,则隐藏等待中  
                        if (count == images.length) {
                            wx.hideToast();
                            console.log('count', count)
                            let data = {
                                user_id: user_id,
                                parent_id: 0, //父帖子ID
                                content: this.data.msg,
                                organization_id: this.data.groupInfo.id, //团ID
                                community_name: this.data.groupInfo.community_name, //	小区名
                                city_id: city_id, //城市ID
                                images: this.data.onLineimages
                            }
                            that.addOrganizationPostBarOne(data)
                        }
                    },
                    fail: function(res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false,
                            success: function(res) {}
                        })
                    }
                });


            }
        } else { //用户 没有上传图片，直接发布动态
            let data = {
                user_id: user_id,
                parent_id: 0, //父帖子ID
                content: this.data.msg,
                organization_id: this.data.groupInfo.id, //团ID
                community_name: this.data.groupInfo.community_name, //	小区名
                city_id: city_id, //城市ID
                images: this.data.onLineimages
            }
            that.addOrganizationPostBarOne(data)
        }
    },
    //朋友圈——发布
    confirmCrony() {
        let that = this;
        let user_id = wx.getStorageSync('user_id');
        let images = this.data.imgs;
        let onLineimages = []; //线上 图片 路径
        let count = 0; //本地图片计数

        if (!this.data.msg) {
            wx.showToast({
                title: '请填写你的动态',
                icon: "none",
                mask: true
            })
            return
        }

        if (images.length) {
            // 先上传图片
            wx.showToast({
                title: '正在上传图片...',
                icon: 'loading',
                mask: true,
                duration: 1500
            })
            for (let i = 0, h = images.length; i < h; i++) {
                console.log('images', images[i])
                    //上传文件
                wx.uploadFile({
                    url: "https://xiangtws.com/public/index.php/api/Status/uploadImages",
                    filePath: images[i],
                    name: 'images[]', //与后台 命名的数组对应
                    success: (res) => {
                        console.log(res)
                        onLineimages.push(res.data)
                        this.setData({
                            onLineimages
                        })
                        count++;
                        //如果是最后一张,则隐藏等待中  
                        if (count == images.length) {
                            wx.hideToast();
                            console.log('count', count)
                            let data = {
                                user_id: user_id,
                                parent_id: 0, //父帖子ID
                                content: this.data.msg,
                                images: this.data.onLineimages
                            }
                            that.addCronyDynamicOne(data)
                        }
                    },
                    fail: function(res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false,
                            success: function(res) {}
                        })
                    }
                });


            }
        } else { //用户 没有上传图片，直接发布动态
            let data = {
                user_id: user_id,
                parent_id: 0, //父帖子ID
                content: this.data.msg,
                images: this.data.onLineimages
            }
            that.addCronyDynamicOne(data)
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //msgType: 1 友邻吧  2 朋友圈
        let msgType = options.msgType
        if (msgType) {
            this.setData({
                msgType
            })
        }

        let groupInfo = wx.getStorageSync('groupInfo');
        if (groupInfo) {
            this.setData({
                groupInfo
            })
        }
    },
    //友邻吧——发布动态
    addOrganizationPostBarOne(data) {
        http.request({
            url: api.addOPostBarOne,
            data: data,
            success: (res) => {
                console.log('发布成功', res);
                if (res.code == 200) {
                    wx.showToast({
                        title: '发布成功',
                        mask: true,
                        icon: 'success',
                        duration: 1500,
                        success: () => {
                            setTimeout(() => {
                                wx.navigateBack({

                                })
                            }, 2000)
                        }
                    })
                } else if (res.code == 201) {

                }

            }
        });
    },

    //朋友圈——发布动态
    addCronyDynamicOne(data) {
        http.request({
            url: api.addCronyDynamicOne,
            data: data,
            success: (res) => {
                console.log('朋友圈东塔-发布成功', res);
                if (res.code == 200) {
                    wx.showToast({
                        title: '发布成功',
                        mask: true,
                        icon: 'success',
                        duration: 1500,
                        success: () => {
                            setTimeout(() => {
                                wx.navigateBack({

                                })
                            }, 2000)
                        }
                    })
                } else if (res.code == 201) {

                }

            }
        });
    },
    onShow: function() {

    },
    onHide: function() {

    },


    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})